(function(app){
	app
		.controller("content_01_listCtrl",["$scope","myFactory","$ionicHistory",'$http','$stateParams','$state',function($scope,myFactory,$ionicHistory,$http,$stateParams,$state){

			//接收参数
			var item=$stateParams.item;
			//var str = JSON.stringify(item);
			var chdrcoy=$stateParams.chdrcoy;
			var itemId=$stateParams.itemId;
			var initFlag = false;
			
			//绑定参数
			$scope.chdrnum01=$stateParams.chdrnum01;
			
			
			$scope.data = {
				allCheck: true, //全选标志
				list: [
				]
			}

/*******************************************************初始化函数begin************************************************************/
			//初始化查询变更项内容
			$scope.load = function () {
				myFactory.loadingShow();//显示加载圈
				var token=sessionStorage.getItem("token");
				var timestamp=myFactory.getTimetamp();
				var sfis= "itemId-token-timestamp";
				var signStr="itemId="+"01"+"&token="+token+"&timestamp="+timestamp;
				var sign = myFactory.hex(signStr);
				$http({
					method: 'GET',
					url: app.domain + 'input/saveContentItem01.action',
					params: {'itemId': '01','itemStr':item,"strSelectedBdh":"",'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						$scope.data.list=response.data.data.itemData.chdrlist
						for (var i = $scope.data.list.length - 1; i >= 0; i--) {
							$scope.data.list[i].checkState = true;
						};
						initFlag = true;
					}
					myFactory.loadingHide();
				}, function errorCallback(response) {
					myFactory.loadingHide();
					myFactory.alert("","初始化查询信息失败!","info");
				});
			}
/*******************************************************初始化函数begin************************************************************/			
			
			//全选控制
			$scope.changeAll = function(){
				console.log($scope.data.allCheck);
				if ($scope.data.allCheck == true) {
					for (var i = $scope.data.list.length - 1; i >= 0; i--) {
						if ($scope.data.list[i].checkState != true) {
							$scope.data.list[i].checkState = true;
						};
					};
				} else{
					for (var i = $scope.data.list.length - 1; i >= 0; i--) {
						if ($scope.data.list[i].checkState == true) {
							$scope.data.list[i].checkState = false;
						};
					};
				};
			}

			$scope.goBack = function () {
				//history.go(-1);
				/*$state.go('content_01_input',{
					changeTypeId:'01'
				});*/
				myFactory.goBack();
			}

			$scope.nextStep = function () {
				//已选择的保单号
				/*for (var i = $scope.data.list.length - 1; i >= 0; i--) {
					if ($scope.data.list[i].checkState == true) {
						console.log("value1:"+$scope.data.list[i]);
						console.log("value2:"+$scope.data.list[i].chdrnum01);
						console.log("value3:"+$scope.data.list[i].rnName);
					};
				};*/
				/*for(var i=0;i<rows.length;i++){
					if(i==0){
						oidid += rows[i].OIDID;
					}else{
						oidid += "," + rows[i].OIDID;
					}
				}*/
				myFactory.loadingShow();//显示加载圈
				//判断是否初始化完成了，如果失败，不予进入项目
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}
				var strSelectedBdh="";
				var flagStatus=false;
				for (var i = $scope.data.list.length - 1; i >= 0; i--) {
					if ($scope.data.list[i].checkState == true) {
						if(flagStatus==false){
							flagStatus=true;
							strSelectedBdh += $scope.data.list[i].chdrnum01;
						 }else{
							 strSelectedBdh += "," + $scope.data.list[i].chdrnum01;
						 }
					}
				}
				flagStatus=false;
				console.log("strSelectedBdh:"+strSelectedBdh);

				item["sourceFlag"] = "2";

//				很明显的重复内容，这一步放到check_main初始化做即可 ZZQ 2017-8-30
//				$http({
//					method: 'GET',
//					url: app.domain + 'input/saveContentItem01.action',
//					params: {'itemId': '01','itemStr':item,"strSelectedBdh":strSelectedBdh}
//				}).then(function successCallback(response) {
//					if (response.data.success) {
//						//$scope.data.list=response.data.data.itemData.chdrlist;
//						$state.go('content_check_main',{
//							itemId:itemId,item:item,chdrnum01:$scope.chdrnum01,chdrcoy:chdrcoy
//						});
//					}
//				}, function errorCallback(response) {
//					alert('查询数据失败!');
//				});
				myFactory.loadingHide();
				$ionicHistory.clearCache().then(function(){ 
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:$scope.chdrnum01,
						chdrcoy:chdrcoy,
						strSelectedBdh:strSelectedBdh //额外穿过去同时要变更的保单项
					});
				})
				
			}

		}])
})(app)